﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using S9Engine;


public partial class Term : Parser
{
    private long _id;
    private int _pageIndex;
    private string _type;
    private string _typeText;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            InitAll();
            
            Session["CurrentPage"] = "Term.aspx";

            if (!CheckLogin()) Response.Redirect("login.aspx");

            _parser.SetCurrentDir("~/html");
            _parser.SetLanguageFile("~/templates/language.txt", _currentLanguage);

            _DR.Refresh(System.Data.Objects.RefreshMode.StoreWins, _DR.Meta);

            _id = Request["id"] == null ? 0 : long.Parse(Request["id"].ToString() ?? "0");

            _type = Request["type"] ?? "hoteltype";

            string termType = string.Empty;

            switch (_type)
            {
                case "style":
                    {
                        _pageIndex = 1;
                        _typeText = "ProductStyle";
                        termType = _parser.GetLabelValue("Style", _currentLanguage);
                        LoadTerm(termType);
                        break;
                    }
                case "color":
                    {
                        _pageIndex = 1;
                        _typeText = "ProductColor";
                        termType = _parser.GetLabelValue("Color", _currentLanguage);
                        LoadTerm(termType);
                        break;
                    }
                case "pattern":
                    {
                        _pageIndex = 1;
                        _typeText = "ProductPattern";
                        termType = _parser.GetLabelValue("Pattern", _currentLanguage);
                        LoadTerm(termType);
                        break;
                    }
                case "material":
                    {
                        _pageIndex = 1;
                        _typeText = "ProductMaterial";
                        termType = _parser.GetLabelValue("Material", _currentLanguage);
                        LoadTerm(termType);
                        break;
                    }
            }
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
        finally
        {
            _parser.Parse("Main");
            _parser.Out("Main");
        }
    }

    private void LoadTerm(string termType)
    {
        LoadFile("term.html", false);

        _parser.Assign("pageIndex", _pageIndex.ToString());

        List<Meta> terms = _DR.Meta.Where(m => m.Type.Equals("product") && m.Key.Equals(_typeText)).ToList();

        foreach (Meta m in terms)
        {
            _parser.Assign("termID", m.ID.ToString());
            _parser.Assign("termName", m.Value);

            _parser.Parse("Main.Content.TermList.TermListItem");
        }

        if (terms.Count > 0) _parser.Parse("Main.Content.TermList");

        _parser.Assign("type", _typeText);
        _parser.Assign("termType", termType);
        _parser.Assign("termTypeLC", termType.ToLower());

        _parser.Parse("Main.Content");
    }
}